package com.ftg.learn.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.ftg.learn.comm.RMessCommEnum;
import com.ftg.learn.dto.LoginDto;
import com.ftg.learn.entity.SysPermission;
import com.ftg.learn.entity.SysUser;
import com.ftg.learn.mapper.SysPermissionMapper;
import com.ftg.learn.mapper.SysUserMapper;
import com.ftg.learn.service.UserService;
import com.ftg.learn.util.JwtUtils;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 布衣
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public R<String> login(LoginDto loginDto,String captcha) {
        if(!captcha.equals(loginDto.getCaptcha())){
            return R.build(RMessCommEnum.R_CAPTCHA_ERROR);
        }
        SysUser sysUser = sysUserMapper.selectOneByUsername(loginDto.getUsername());
        if(Objects.isNull(sysUser)){
            return R.build(RMessCommEnum.R_USER_NOTFOUND);
        }

        //密码应该加密以后再传递
        if(!sysUser.getPassword().equals(loginDto.getPassword())){
            return R.build(RMessCommEnum.R_PASSWORD_NOTFOUND);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("username", sysUser.getUsername());
        map.put("id", sysUser.getId());

        return R.ok(JwtUtils.generateToken(sysUser.getUsername(), map));
    }

    @Override
    public R<List<Tree<Long>>> getCurUserPerById(String token) {

        if(token==null){
            return R.failure();
        }

        if(JwtUtils.validateToken(token)){
            Claims claims = JwtUtils.parseToken(token);
            Long id = Long.valueOf(claims.get("id").toString());
            List<SysPermission> list = sysPermissionMapper.listById(id);

            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("id");
            treeNodeConfig.setParentIdKey("parentId");
            treeNodeConfig.setNameKey("name");
            treeNodeConfig.setChildrenKey("children");

            List<Tree<Long>> build = TreeUtil.build(list, 0L, treeNodeConfig,(treeNode,tree)->{
                tree.setId(treeNode.getId());
                tree.setParentId(treeNode.getParentId());
                tree.setName(treeNode.getName());
                tree.putExtra("url",treeNode.getUrl());
                tree.putExtra("icon",treeNode.getIcon());
                tree.putExtra("type",treeNode.getType());
            });

            return R.ok(build);
        }

        return R.build(RMessCommEnum.R_MESS_NO_TOKEN);
    }

    @Override
    public R<PageEx<SysUser>> list(String username, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return R.ok(PageEx.convertToPageEx(new PageInfo<>(sysUserMapper.list(username))));
    }


}
